package com.malbbmsomanager.management;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.malbbmsomanager.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class DM_BBM_Items_Prices_Information extends Activity {
	private static final String TAG = null;
	private SQLiteDatabase mydb;
	private String DBNAME = "dminvoicemanagerbbm.db";
	String CurrentRefItem;

	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_BBM_Item_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}
	SimpleDateFormat originalFormat = new SimpleDateFormat("dd MM yyyy");
	SimpleDateFormat targetFormat = new SimpleDateFormat("dd/MM/yyyy");
	Date settingDate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_bbm_items_prices_information);

		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Price Revisions");
		selectCurrentItemCode();
	}

	private void selectCurrentItemCode() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentItems = null;
		try {
			String query = "SELECT * from FItemsMasterSum";
			curCurrentItems = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentItems != null) {
			if (curCurrentItems.getCount() > 0) {
				while (curCurrentItems.moveToNext()) {
					CurrentRefItem = curCurrentItems.getString(1);
					Log.v(TAG, "CurrentRefItem XXX :" + CurrentRefItem);
					filterItemsPriceRevision();
				}
			}
		}
		curCurrentItems.close();
		mydb.close();
	}

	private void filterItemsPriceRevision() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dmitemspricerevisionlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "colitempri_1", "colitempri_2", "colitempri_3" };
		int[] totrdes = new int[] { R.id.item_pri_name,
				R.id.item_pri_date, R.id.item_pri_value };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor chkItemdPrice = null;
		try {
			/*String queryChkItemInfo = "SELECT FI.ItemCode,FI.ItemName,IP.PriRDate,IP.Price,IP.PrilCode FROM FItems FI,fItemPri IP Where FI.ItemCode = IP.ItemCode AND FI.ItemCode = '"
					+ CurrentRefItem + "' AND IP.PrilCode = 'WHOLE SALE'";*/
			String queryChkItemInfo = "SELECT fi.ItemCode,fi.ItemName,fi.AddDate,ip.ItemCode,ip.Price,ip.PrilCode FROM FItems fi,fItemPri ip WHERE fi.ItemCode = ip.ItemCode AND fi.ItemCode = '" + CurrentRefItem + "'";
			chkItemdPrice = mydb.rawQuery(queryChkItemInfo, null);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (chkItemdPrice != null) {
			if (chkItemdPrice.getCount() > 0) {
				while (chkItemdPrice.moveToNext()) {
					String strItemName = chkItemdPrice.getString(1);
					String strPriRDate = chkItemdPrice.getString(2);
					String[] separated = strPriRDate.split(" ");
					strPriRDate = separated[0]; // this will contain Date Part
					
					try {
						settingDate = originalFormat.parse(strPriRDate.replaceAll(
								"[-+/.^:,]", " "));
						strPriRDate = targetFormat.format(settingDate);
					} catch (ParseException ex) {
						Log.v(TAG, "Exception ****** :" + ex);
						strPriRDate = getDeviceTaxnDate().toString();
					}
					
					String strPrice = chkItemdPrice.getString(4);
					BigDecimal strPriceVal = new BigDecimal(strPrice.replaceAll("[,]", ""));
					strPriceVal = strPriceVal.setScale(2, BigDecimal.ROUND_HALF_DOWN);
					String strPriceListCode = chkItemdPrice.getString(5);
					//WHOLE SALE
					if(strPriceListCode.equals("WHOLE SALE")){
						strPriceListCode = "WSP";
					}
					else {
						strPriceListCode = "RRP";
					}
					Log.v(TAG, "PL_CODE XXX :" + strPriceListCode);
					
					HashMap<String, String> maptrdes = new HashMap<String, String>();
					maptrdes.put("colitempri_1", strItemName);
					maptrdes.put("colitempri_2", strPriRDate);
					maptrdes.put("colitempri_3", "" +strPriceVal + "(" + strPriceListCode + ")");
					fillMapstrdes.add(maptrdes);
				}
				// fill in the grid_item layout
				SimpleAdapter adaptertrdes = new SimpleAdapter(this,
						fillMapstrdes, R.layout.dm_bbm_items_prices_information_raw,
						fromtrdes, totrdes);
				lvtypedes.setAdapter(adaptertrdes);
			}
		}
		chkItemdPrice.close();
		mydb.close();
	}
	private String getDeviceTaxnDate() {
		Calendar cad = Calendar.getInstance();
		SimpleDateFormat dfad = new SimpleDateFormat("dd/MM/yyyy");
		String formattedAddDate = dfad.format(cad.getTime());
		return formattedAddDate;
	}
	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
